package com.wanxi.dao.impl;

import com.wanxi.dao.ProductDao;
import com.wanxi.model.ProductModel;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    @Override
    public int add(ProductModel model) {
        String sql = "insert into product (" +
                "name,price,normal_price," +
                "content,img_href,cuisine_id," +
                "enable) " +
                "values('" + model.getName() + "','" + model.getMarketPrice() + "','" + model.getNormalPrice() + "'," +
                "'" + model.getContent() + "','" + model.getImgHref() + "','" + model.getCuisineId() + "'," +
                "'" + model.getEnable() + "')";

        return JDBC.update(sql);
    }

    @Override
    public int update(ProductModel model) {
        return 0;
    }

    @Override
    public int delete(ProductModel model) {
        return 0;
    }

    @Override
    public List<ProductModel> findAll(ProductModel model) {
        int start = (model.getPage() - 1) * model.getLimit();
        String sql = " select * from (" +
                " select p.* ,c.name cuisine_name from product p" +
                " left join cuisine c" +
                " on p.cuisine_id=c.id" +
                " order by p.id desc " +
                " ) t where 1=1";
        sql += getSql(model);
        sql += " limit " + start + "," + model.getLimit();
        ResultSet query = JDBC.query(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (query.next()) {
                ProductModel productModel = ProductModel.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .marketPrice(query.getBigDecimal("price"))
                        .normalPrice(query.getBigDecimal("normal_price"))
                        .imgHref(query.getString("img_href"))
                        .enable(query.getInt("enable"))
                        .cuisineId(query.getInt("cuisine_id"))
                        .createTime(query.getString("create_time"))
                        .cuisineName(query.getString("cuisine_name"))
                        .build();
                list.add(productModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    private String getSql(ProductModel model) {
        String sql = "";
        if (!"".equals(model.getName()) && model.getName() != null) {
            sql += " and p.name like '%" + model.getName() + "%'";
        }
        if (model.getCuisineId() != -1) {
            sql += " and p.cuisine_id =" + model.getCuisineId();
        }
        return sql;
    }

    @Override
    public int count(ProductModel model) {
        String sql = " select count(*) count from (" +
                " select p.* ,c.name cuisine_name from product p" +
                " left join cuisine c" +
                " on p.cuisine_id=c.id" +
                " order by p.id desc " +
                " ) t where 1=1";
        sql += getSql(model);
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                return query.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public ProductModel findById(ProductModel model) {
        String sql="select * from product where id="+model.getId();
        ProductModel productModel=null;
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                productModel= ProductModel.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .marketPrice(query.getBigDecimal("price"))
                        .normalPrice(query.getBigDecimal("normal_price"))
                        .imgHref(query.getString("img_href"))
                        .enable(query.getInt("enable"))
                        .cuisineId(query.getInt("cuisine_id"))
                        .createTime(query.getString("create_time"))
                        .content(query.getString("content"))
                        .build();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return productModel;
    }
}
